<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->
<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<div class="execution">
				<avue-crud
					ref="crud"
					v-model="form"
					:page="page"
					:data="tableData"
					:table-loading="tableLoading"
					:option="tableOption"
					:before-open="handleOpenBefore"
					:permission="permissionList"
					@on-load="getList"
					@search-change="searchChange"
					@search-reset="searchreset"
					@refresh-change="refreshChange"
					@size-change="sizeChange"
					@current-change="currentChange"
					@row-update="handleUpdate"
					@row-save="handleSave"
					@row-del="rowDel"
					@selection-change="handleSelectionChange"
				>
					<template #menu-left="{}">
						
						<el-button class="filter-item"  plain v-auth="'access_accessSupplier_export'"  icon="upload-filled" type="primary" @click="$refs.excelUpload.show()">导入 </el-button>
						<el-button class="filter-item" v-auth="'access_accessSupplier_import'"  type="primary" icon="delete" :disabled="multiple" @click="handleDelete(selectObjs)">删除 </el-button>
					</template>
					<template #menu-right="{}">
						<el-button class="filter-item"  v-auth="'access_accessSupplier_export'" circle icon="Download"  @click="exportExcel"></el-button>
					</template>
					<template #supplierName-search>
						<avue-input v-model="searchForm.supplierName" placeholder="请输入 供应商名称" @keyup.enter="searchChange"></avue-input>
					</template>
				
				</avue-crud>
				<!--excel 模板导入 -->
				<excel-upload
                ref="excelUpload"
                title="供应商信息导入"
                url="/admin/bamaassetssupplier/import"
                temp-name="供应商信息.xlsx"
                temp-url="/admin/sys-file/local/file/supplier.xlsx"
                @refreshDataList="handleRefreshChange"
            ></excel-upload>
			</div>
		</div>
	</div>
</template>


<script>
import { fetchList, addObj, putObj, delObj, getSupplierNumber } from '/@/api/basiciot/device/supplier';
import { tableOption } from '/@/crud/basiciot/device/supplier';



import ExcelUpload from '/@/components/ExcelUpload/index.vue'

import { storeToRefs } from 'pinia';
import { useUserInfo } from '/@/stores/userInfo';
import other from '/@/utils/other';

export default {
	name: 'bamsupplier',
	components:{ExcelUpload },
	data() {
		return {
			form: {},
			searchForm: {},
			tableData: [],
			page: {
				total: 0, // 总页数
				currentPage: 1, // 当前页数
				pageSize: 10, // 每页显示多少条
			},
			tableLoading: false,
			tableOption: tableOption,
			multiple: true,
			selectObjs: []
		};
	},
	computed: {
		permissionList() {
			return {
				addBtn: this.userInfos.some((v) => v === 'access_accessSupplier_add'),
				delBtn: this.userInfos.some((v) => v === 'access_accessSupplier_del'),
				editBtn: this.userInfos.some((v) => v === 'access_accessSupplier_edit'),
			};
		}
	},
	created(){
		// 定义变量内容
		const stores = useUserInfo();
		const { userInfos } = storeToRefs(stores);
		this.userInfos = userInfos.value.authBtnList
	},
	mounted() {
		
	},
	methods: {
		searchreset() {
			this.searchForm = {};
		},
		handleOpenBefore(show, type) {
			if (type === 'add') {
				getSupplierNumber().then((res) => {
					this.form.supplierCode = res.data;
				});
			}
			show();
		},
		getList(page, params) {
			this.tableLoading = true;
			fetchList(
				Object.assign(
					{
						current: page.currentPage,
						size: page.pageSize,
					},
					params,
					this.searchForm
				)
			)
				.then((response) => {
					this.tableData = response.data.records;
					this.page.total = response.data.total;
					this.tableLoading = false;
				})
				.catch(() => {
					this.tableLoading = false;
				});
		},
		rowDel: function (row, index) {
			this.$confirm('是否确认删除名称为' + row.supplierName + '的供应商？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(function () {
					return delObj([row.supplierId]);
				})
				.then((data) => {
					this.$message.success('删除成功');
					this.getList(this.page);
				})
				.catch((cancelorerror) => {});
		},
		handleDelete (objs) {
			this.$confirm('此操作将永久删除', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(function () {
					return delObj(objs);
				})
				.then((data) => {
					this.$message.success('删除成功');
					this.getList(this.page);
				})
				.catch((cancelorerror) => {});
		},
		handleUpdate: function (row, index, done, loading) {
			putObj(row)
				.then((data) => {
					this.$message.success('修改成功');
					done();
					this.getList(this.page);
				})
				.catch(() => {
					loading();
				});
		},
		handleSave: function (row, done, loading) {
			addObj(row)
				.then((data) => {
					this.$message.success('添加成功');
					done();
					this.getList(this.page);
				})
				.catch(() => {
					loading();
				});
		},
		sizeChange(pageSize) {
			this.page.pageSize = pageSize;
		},
		currentChange(current) {
			this.page.currentPage = current;
		},
		searchChange(form, done) {
			if (!this.searchForm.supplierName) {
				delete this.searchForm.supplierName;
			}
			//this.searchForm = form
			this.page.currentPage = 1;
			this.getList(this.page, form);
			if (done) {
				done();
			}
		},
		refreshChange() {
			this.getList(this.page);
		},
		handleRefreshChange() {
			this.getList(this.page);
		},
		// 多选事件
		handleSelectionChange(objs) {
			this.selectObjs = objs.map(({ supplierId }) => supplierId);
			this.multiple = !objs.length;
		},
		exportExcel() {
			other.downBlobFile(other.adaptationUrl('/bm/accessSupplier/export'), this.searchForm, '供应商信息.xlsx');
		},
	},
};
</script>
